#ifndef __I2DNODE_H__
#define __I2DNODE_H__

#include "PowerLibraryEngine.h"

class I2DNode
{
protected:
    stringc FName;
    IImage *img;
    ITexture *txt;
    I2DSpriteSceneNode *spr;
public:
    I2DNode():img(NULL),txt(NULL),spr(NULL),FName("")
    {
    }

    virtual ~I2DNode()
	{
		Destroy();
	}

	virtual void Destroy()
	{
		DEALLOCATE(img);

		pLogDebug << "Deallocating I2DNode " << FName << "..." << endl;
		TextureManager->DeleteTexture(txt);
		DEALLOCATE(spr);
		pLogDebug << "I2DNode " << FName << " deallocated" << endl;

	}

    virtual bool IsLoaded()
    {
        return (img && txt && spr);
    }

    virtual bool LoadNode(stringc FileName)
    {
        FName = FileName;
        img = PLE_FileSystem->GetImage(FName);
//        txt = TextureManager->CreateTextureFromImage(img);
        txt = TextureManager->CreateTextureBeta(img);
        spr = SceneManager->GetSceneGenerator()->GetNew2DSpriteSceneNode(txt);
        spr->SetName(FileName);
        return IsLoaded();
    }

    virtual bool CreateEmptyNode(CColor color)
    {
        img = NULL;
//        txt = TextureManager->CreateEmptyTexture(color);
        txt = TextureManager->CreateTextureBeta(NULL,color);
        spr = SceneManager->GetSceneGenerator()->GetNew2DSpriteSceneNode(txt);
        return true;
    }

    virtual bool Copy(I2DNode *Other)
    {
        img = Other->GetImage()->Clone();
        txt = TextureManager->CreateTextureFromImage(img);
        spr = SceneManager->GetSceneGenerator()->GetNew2DSpriteSceneNode(txt);
        spr->SetName(Other->GetSpriteNode()->GetName());
        return IsLoaded();
    }

    virtual bool Clone(I2DNode *Other)
    {
        img = Other->GetImage()->Clone();
        txt = TextureManager->CloneTexture(Other->GetTexture());
        spr = SceneManager->GetSceneGenerator()->GetNew2DSpriteSceneNode(txt);
        spr->SetName(Other->GetSpriteNode()->GetName());
        return IsLoaded();
    }

	virtual bool ChangeImage(IImage *newImg)
	{
    	if (img)
			DEALLOCATE(img);

		img = newImg->Clone();
		txt = TextureManager->CreateTextureFromImage(img);
		spr->SetTextures(txt);
		return IsLoaded();
	}

    virtual bool ChangeImage(I2DNode *newNode)
    {
    	ChangeImage(newNode->GetImage());
    	spr->SetName(newNode->GetSpriteNode()->GetName());
    	return IsLoaded();
    }

    virtual void SetLenPropRelative(vPos LenX=100.0,vPos LenY = 100.0,vPos LenZ = 1.0)
    {
        vPos ZoomX = (vPos)(LenX / 50.0);
        vPos ZoomY = (vPos)(LenY / 50.0);
        spr->SetScale(ZoomX,ZoomY,1.0);
    }

    virtual void SetLenPropRelative(vector2dvpos Len=vector2dvpos(100.0,100.0))
    {
        SetLenPropRelative(Len.X,Len.Y);
    }

    virtual void SetLenPixelsRelative(vPos LenX = 100.0,vPos LenY = 100.0)
    {

    }

    virtual void SetLenPixelsRelative(vector2dvpos Len=vector2dvpos(100.0,100.0))
    {
        SetLenPixelsRelative(Len.X,Len.Y);
    }

    virtual I2DSpriteSceneNode *GetSpriteNode()
    {
        return spr;
    }

    virtual IImage *GetImage()
    {
        return img;
    }

    virtual ITexture *GetTexture()
    {
        return txt;
    }

    virtual stringc GetFName()
    {
        return FName;
    }
};

#endif
